import { hashString } from '../utils';
/**
 * 
 * @param {*} args 数组 [['color:red']]
 */
function serializeStyles(args, props) {
  if (typeof args === 'object' && args.styles !== undefined) {
    return args;
  }
  let styles = '';
  const strings = args[0];
  if (strings.raw === undefined) {//说明传的是对象
    styles += handleInterpolation(strings, props);
  } else {
    styles += strings[0];//styles="color:red"
  }
  for (let i = 1; i < args.length; i++) {
    styles += handleInterpolation(args[i], props);
  }
  const name = hashString(styles);
  return {
    name,
    styles
  };
}
function handleInterpolation(interpolation, props) {
  switch (typeof interpolation) {
    case 'object': {
      return createStringFromObject(interpolation);
    }
    case 'function': {
      var result = interpolation(props);
      return handleInterpolation(result);
    }
  }
}
function createStringFromObject(obj) {
  let string = '';
  for (const key in obj) {
    string += (key + ":" + obj[key] + ";");
  }
  return string;
}
export default serializeStyles;